package com.ddy.test.creditcardmanage.model

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.ddy.test.creditcardmanage.db.CardDatabase
import com.ddy.test.creditcardmanage.repository.CardRepository

class CardViewModel(app: Application): AndroidViewModel(app) {
    private val repo : CardRepository = CardRepository(CardDatabase.getInstance(app)!!.cardDao())

    fun getAllCards() : LiveData<List<Card>> {
        return repo.getAllCards()
    }

    fun getCard(id: Long): Card {
        return repo.getCard(id)
    }

    fun addCard(card: Card){
        return repo.addCard(card)
    }

    fun deleteCard(id: Long){
        repo.deleteCardRecordByCard(id)
        return repo.deleteCard(id)
    }

    fun updateCard(card: Card) {
        return repo.updateCard(card)
    }

    fun getAllCardRecords(cardId: Long): LiveData<List<CardRecord>>{
        return repo.getAllCardRecords(cardId)
    }

    fun getCardRecordsByTime(cardId: Long, from: Long, to: Long): LiveData<List<CardRecord>>{
        return repo.getCardRecordsByTime(cardId, from, to)
    }

    fun addCardRecord(cardRecord: CardRecord){
        return repo.addCardRecord(cardRecord)
    }

    fun deleteCardRecord(id: Long){
        return repo.deleteCardRecord(id)
    }
}